<?php


namespace app\model;


use think\facade\Cache;
use think\Model;

class Area extends Model
{

    /**
     * 获取省市区地址
     * @param $data
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArea($data)
    {
        $redis = Cache::store('redis');
        if (!$redis->get('get_area')) {
            $arr = $this->field('id,pid,name,first,pinyin,showFlag')->select()->toArray();
            $redis->set('get_area', json_encode($arr));
        } else {
            $arr = json_decode($redis->get('get_area'), true);
        }
        $arr = tree($arr);
        if (isset($data['pid']) || $data['pid']) {
            $arrs = array_column($arr, NULL, 'id');
            $arr = $arrs[$data['pid']]['child'];
        }
        return app('json')->json_success('获取成功', $arr);
    }

}